package cn.yunhe.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.ModelAndView;

import cn.yunhe.dao.DeptDao;
import cn.yunhe.dao.EmpDao;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import cn.yunhe.service.EmpService;

@Controller
public class EmpController {
	@Autowired
	private EmpService empService;
	@Autowired
	private EmpDao empDao;
	@Autowired
	private DeptDao deptDao;

	/*
	 * @RequestMapping("/login") public ModelAndView findDrugAll(){ ModelAndView
	 * mav = new ModelAndView("success");
	 * 
	 * return mav; }
	 */
	/**
	 * 登录
	 * 
	 * @param ename
	 * @param pwd
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String ename, String pwd, HttpServletRequest request) {
		Emp emp = empService.login(ename, pwd);
		HttpSession session = request.getSession();
		session.getAttribute(ename);
		// session.setAttribute("name", ename);
		request.getSession().setAttribute("name", ename);
		System.out.println("===========================" + ename + "========================");
		if (emp != null) {
			System.err.println(emp);
			session.setAttribute("CURR_USER", emp);
			return "../../home";
		} else {
			request.setAttribute("msg", "用户名或密码错误!!");
			return "../../login";
			
		}

	}

	/**
	 * 添加员工
	 * 
	 * @param ename
	 * @param epwd
	 * @param deid
	 * @param request
	 * @return
	 */
	@RequestMapping("/registy")
	public String registy(String ename, String epwd, int deid, HttpServletRequest request,@RequestParam("upload")MultipartFile file,Model model) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		 String path = request.getServletContext().getRealPath("/image");
		 File targetFile1 = new File(path, file.getOriginalFilename());
		 File targetFile = new File(path,System.currentTimeMillis()+".jpg");
		 model.addAttribute("fname", file.getOriginalFilename());
		 String eimg=(System.currentTimeMillis()+"").trim();
			try {
				file.transferTo(targetFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEpwd(epwd);
		emp.setDeid(deid);
		emp.setEimg(eimg);
		emp.setEstate(1);
		emp.setElev(1);
		int num = empDao.registy(emp);
		if (num == 1) {
			request.setAttribute("msg", "添加成功");
			return "../../registy";
		} else {
			request.setAttribute("msg", "添加失败");
			return "../../registy";
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param eid
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(int eid, HttpServletRequest request) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		HttpSession session = request.getSession();
		System.out.println(session.getSessionContext().toString());
		System.out.println(session.getAttribute("name"));
		String name = session.getAttribute("name").toString();
		Emp emp = empDao.getLev(name);
		int lev = emp.getElev();
		System.out.println("用户等级:" + lev);
		int num = empDao.delete(eid);
		if (num == 1) {
			select(request);
			return "../../index";
		} else {
			request.setAttribute("msg", "删除失败");
			return "../../index";
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param ename
	 * @param epwd
	 * @return
	 */
	@RequestMapping("/updatePwd")
	public String updatePwd(String epwd, HttpServletRequest request) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		String ename = request.getSession().getAttribute("name").toString();
		Emp emp = empDao.getEmpByEname(ename);
		emp.setEpwd(epwd);
		System.out.println(ename + "==============================" + epwd);
		int num = empDao.updatePwd(emp);
		System.out.println("num:" + num);
		if (num == 1) {
			getEmpByEname(request);
			return "../../personal";
		} else {
			return "../../updatePwd";
		}
	}

	/**
	 * 退出登录
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logoutUser(HttpServletRequest request) {
		System.out.println("==========注销===========");
		HttpSession session = request.getSession();
		session.getAttribute("name");
		System.out.println(session.getSessionContext().toString());
		System.out.println(session.getAttribute("name"));
		session.invalidate();
		if ("name" == null) {
			System.out.println("该用户不存在");
			return "success";
		} else {
			System.out.println("退出成功");
			return "../../login";
		}

	}

	/**
	 * 查看医生信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request) {
		System.out.println("================================1111111111111");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		HttpSession session = request.getSession();
		System.out.println(session.getAttribute("name"));
		String name = session.getAttribute("name").toString();
		Emp emp = empDao.getLev(name);
		int lev = emp.getElev();
		System.out.println("用户等级:" + lev);
		if (lev == 2) {
			List<Emp> list = empDao.findEmpsByLev();
			for (Emp emp2 : list) {
				System.out.println(emp2.getEname() + "," + emp2.getDept().getDename());
			}
			request.setAttribute("list", list);
			return "../../index";
		} else {
			request.setAttribute("msg", "亲 你的权限不足，请进行其他操作，想获得权限，请咨询郑金鹏先生");
			return "success";
		}

	}
	@RequestMapping("/select2")
	public String select2(HttpServletRequest request,HttpSession session) {
		System.out.println("============select2()!!!!!!!===================");
	
		Emp emp = (Emp) session.getAttribute("CURR_USER");
		System.out.println("用户等级:" + emp.getElev());
		if (emp.getElev() == 2) {
			
			return "../../registy";
		} else {
			request.setAttribute("msg", "亲 你的权限不足，无法添加员工，请进行其他操作，想获得权限，请咨询郑金鹏先生");
			return "success";
		}

	}
	/**
	 * 根据员工号修改部门
	 * 
	 * @param eid
	 * @param deid
	 * @return
	 */
	@RequestMapping("/updateDeptByEid")
	public String updateDeptByEid(int eid, int deid) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		int num = empDao.updateDeptByEid(eid, deid);
		if (num == 1) {
			return "success";
		} else {
			return "../../login";
		}
	}

	/**
	 * 根据员工号查询个人信息
	 * 
	 * @param eid
	 * @param request
	 * @return
	 */
	@RequestMapping("/getEmpByEid")
	public String getEmpByEid(int eid, HttpServletRequest request) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		Emp emp = empDao.getEmpByEid(eid);
		System.out.println("用户名:" + emp.getEname() + "," + emp.getDept().getDename());
		request.setAttribute("emp", emp);
		DeptDao deptDao = (DeptDao) ac.getBean("deptDao");
		List<Dept> list2 = deptDao.findDeptByState();
		for (Dept d : list2) {
			System.out.println(d.getDeid() + "," + d.getDename());
		}
		request.setAttribute("list2", list2);
		return "../../updateEmp";
	}

	/**
	 * 根据员工号修改员工信息
	 * 
	 * @param eid
	 * @param epwd
	 * @param ename
	 * @param deid
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateEmpByEid")
	public String updateEmpByEid(int eid, String epwd, String ename, int deid, HttpServletRequest request,@RequestParam("upload")MultipartFile file,Model model) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		String path = request.getServletContext().getRealPath("/image");
		 File targetFile1 = new File(path, file.getOriginalFilename());
		 File targetFile = new File(path,System.currentTimeMillis()+".jpg");
		 model.addAttribute("fname", file.getOriginalFilename());
		 String eimg=(System.currentTimeMillis()+"").trim();
			try {
				file.transferTo(targetFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		System.out.println("=========================!!!!");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		Emp emp = empDao.getEmpByEid(eid);
		emp.setEname(ename);
		emp.setEpwd(epwd);
		emp.setDeid(deid);
		emp.setEimg(eimg);
		int num = empDao.updateEmpByEid(emp);
		System.out.println("num:" + num);
		if (num == 1) {
			System.out.println("11111");
			select(request);
			return "../../index";
		} else {
			request.setAttribute("msg", "修改失败");
			return "../../updateEmp";
		}
	}

	/**
	 * 根据员工名字获取员工信息
	 * 
	 * @return
	 */
	@RequestMapping("/getEmpByEname")
	public String getEmpByEname(HttpServletRequest request) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		String ename = request.getSession().getAttribute("name").toString();
		Emp emp = empDao.getEmpByEname(ename);
		System.out.println(emp.getEname() + "," + emp.getDept().getDename());
		request.setAttribute("emp", emp);
		return "../../personal";
	}

	/**
	 * 根据员工名字获取员工信息
	 * 
	 * @param ename
	 * @param request
	 * @return
	 */
	@RequestMapping("/getEmpBy")
	public String getEmpBy(String ename, HttpServletRequest request) {
		/*ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		System.out.println("获取到的用户名:" + ename+",,========");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		Emp emp = empDao.getEmpBy(ename);
		System.out.println("获取到的用户名:" + ename);
		request.setAttribute("elev", emp.getEname());
		selectEmp(request);*/
		System.out.println("获取到的用户名:" + ename+",,==========");
		List<Emp> elist = empService.findEmpAllByLikeName("%"+ename+"%");
		request.setAttribute("list", elist);
		return "../../index";
	}

	@RequestMapping("/selectEmp")
	public String selectEmp(HttpServletRequest request) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		EmpDao empDao = (EmpDao) ac.getBean("empDao");
		String ename = request.getAttribute("elev").toString();
		List<Emp> list = empDao.selectEmp(ename);
		for (Emp e : list) {
			System.out.println(e.getEname() + "," + e.getDept().getDename());
		}
		request.setAttribute("list", list);
		return "../../index";

	}

	// 权威
	@RequestMapping("/findEmpImg")
	public String findEmpImg(HttpServletRequest request) {

		List<Emp> list = empService.findEmpAll();
		for (Emp e : list) {
			System.out.println(e.getEname() + "," + e.getDept().getDename() + ",," + e.getEimg());
		}
		request.setAttribute("list", list);
		return "empImg";

	}

	// 个人 中心
	@RequestMapping("/getUserCenter")
	public String getUserCenter(HttpServletRequest request, HttpSession session) {
		System.out.println("================个人中心!!!========");
		Emp emp = (Emp) session.getAttribute("CURR_USER");
		System.out.println("emp:::"+emp+",,"+emp.getEid());
		
		Emp emp2 = empService.findEmpById(emp.getEid());
		System.out.println(",,"+emp2);
		request.setAttribute("emp", emp2);
		return "../../userCenter";

	}
	// 个人 中心
		@RequestMapping("/userUpdateByEid")
		public String userUpdateByEid(String pwd,HttpServletRequest request, HttpSession session) {
			System.out.println("================修改密码!!!============");
			Emp emp = (Emp) session.getAttribute("CURR_USER");
			System.out.println("emp:::"+emp+",,"+emp.getEid());
			
			int num = empService.updateEmpByEmpId(emp.getEid(), pwd);
			// 查询一次
			Emp emp2 = empService.findEmpById(emp.getEid());
			System.out.println(",,"+emp2);
			request.setAttribute("emp", emp2);
			System.out.println("num,,"+num);
			return "../../userCenter";

		}

	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	public EmpDao getEmpDao() {
		return empDao;
	}

	public void setEmpDao(EmpDao empDao) {
		this.empDao = empDao;
	}

	public DeptDao getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}

}
